﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing.Fields;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace Macaw.Moss2007.FieldContentPanelSolution
{
    /// <summary>
    /// Based on: http://www.andrewconnell.com/blog/archive/2009/07/17/Hiding-Empty-Content-Areas-in-SharePoint-2007-WCM-Publishing-Sites.aspx
    /// </summary>
    public class FieldContentPlaceholder : Macaw.Wss3.FieldContentPanelSolution.FieldContentPlaceholder
    {

        /// <summary>
        /// This method will check if the given field with the fieldvalue is to be seen as empty
        /// </summary>
        /// <param name="field">The field-definition</param>
        /// <param name="fieldvalue">The fieldvalue</param>
        /// <returns>"True" if empty</returns>
        protected override bool FieldEmpty(SPField field, object fieldvalue)
        {
            if (fieldvalue is ContentTypeIdFieldValue)
            {
                ContentTypeIdFieldValue typedvalue = (ContentTypeIdFieldValue)fieldvalue;
                return typedvalue.IsEmpty;
            }
            else if (fieldvalue is ImageFieldValue)
            {
                ImageFieldValue typedvalue = (ImageFieldValue)fieldvalue;
                return string.IsNullOrEmpty(typedvalue.ImageUrl);
            }
            else if (fieldvalue is LinkFieldValue)
            {
                LinkFieldValue typedvalue = (LinkFieldValue)fieldvalue;
                return string.IsNullOrEmpty(typedvalue.Text) && string.IsNullOrEmpty(typedvalue.NavigateUrl);
            }
            else if (fieldvalue is SummaryLinkFieldValue)
            {
                SummaryLinkFieldValue typedvalue = (SummaryLinkFieldValue)fieldvalue;
                return (typedvalue.SummaryLinks == null) || (typedvalue.SummaryLinks.Count == 0);
            }
            else
            {
                // Check FieldEmpty of base (WSS3)
                return base.FieldEmpty(field, fieldvalue);
            }
        }
        
    }
}
